/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TCarfixshop;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderCarFixRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.OrderCarFixResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderCarFixResponseHandler;
import com.gmail.blackdog1987.carcare.widget.TimePicker;

/**
 * @author wdy
 * 
 */
public class OrderCarFixActivity extends BaseActivity implements OnClickListener, OnResponseListener {
	private Context mContext;
	private EditText mName;
	private TextView mTitle;
	private EditText mPhone;
	private EditText mInfo;
	private TextView mTime;
	private CubeImageView mHead;
	private long time;
	private View mPay;
	private OrderCarFixRequestBuilder mReq = new OrderCarFixRequestBuilder();
	private OrderCarFixResponseHandler mHandler = new OrderCarFixResponseHandler();
	TCarfixshop shop;
	ImageLoader imageLoader;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_fix);
		mContext = this;
		imageLoader = ImageLoaderFactory.create(mContext);
		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		mTitle = (TextView) findViewById(R.id.order_carfix_title);
		mName = (EditText) findViewById(R.id.order_carfix_name);
		mPhone = (EditText) findViewById(R.id.order_carfix_phone);
		mTime = (TextView) findViewById(R.id.order_carfix_time);
		mPay = findViewById(R.id.order_carfix_pay);
		mInfo = (EditText) findViewById(R.id.order_carfix_info);
		mHead = (CubeImageView) findViewById(R.id.order_carfix_head);
	}

	public void reg() {
		findViewById(R.id.back).setOnClickListener(this);
		mPay.setOnClickListener(this);
		mTime.setOnClickListener(this);
		mHandler.setOnResponseListener(this);
		findViewById(R.id.order_carfix_call).setOnClickListener(this);
		findViewById(R.id.order_carfix_location).setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		shop = (TCarfixshop) getIntent().getSerializableExtra("data");
		mTitle.setText(shop.getTitle());
		if (shop.headurl != null && shop.headurl.length() != 0) {
			mHead.loadImage(imageLoader, shop.headurl);
		}
	}

	private boolean validInput() {
		return true;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.order_carfix_call:
			if (shop.phone == null || shop.phone.length() == 0) {
				Toast.makeText(mContext, "尚未录入商家电话", Toast.LENGTH_LONG).show();
				return;
			}
			Intent callintent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + shop.phone));
			startActivity(callintent);

			break;
		case R.id.order_carfix_location:
			Intent intent = new Intent(OrderCarFixActivity.this, MapGuidActivity.class);
			intent.putExtra("lat", shop.lat);
			intent.putExtra("lng", shop.lon);
			startActivity(intent);
			break;
		case R.id.back:
			finish();
			break;
		case R.id.order_carfix_time:
			Intent startIntent = new Intent(OrderCarFixActivity.this, TimePicker.class);
			startActivityForResult(startIntent, R.id.order_carfix_time);
			break;
		case R.id.order_carfix_pay:
			if (!App.validUser(this))
				return;
			if (validInput()) {
				mReq = new OrderCarFixRequestBuilder();
				mReq.setPhone(mPhone.getText().toString()).setShopid(String.valueOf(shop.getId())).setTime(String.valueOf(time))
						.setInfo(mInfo.getText().toString()).setName(mName.getText().toString()).postJsonRequest(mContext, mHandler);
			}
			break;
		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case R.id.order_carfix_time:
				time = data.getLongExtra("time", -1);
				if (time != -1) {
					Date date = new Date(time);
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					String tmp = format.format(date);
					mTime.setText(tmp);
				}
				break;
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
				finish();
				break;
			}
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		// Toast.makeText(mContext, "", Toast.LENGTH_LONG).show();
		// finish();
		TProduct product = new TProduct();
		product.body = "维修";
		product.price = Double.valueOf(shop.getPrice());
		product.subject = shop.getTitle();
		product.table = "t_order_carfix";
		product.tableId = ((OrderCarFixResponse) classz.getResponse()).order.id;
		product.userid = new UserDao().getUser().id;
		Intent i = new Intent(OrderCarFixActivity.this, PayActivity.class);
		i.putExtra("data", product);
		startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败！", Toast.LENGTH_LONG).show();
	}
}
